package com.lonely.fx.tree_view;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @date: 2022-08-04 10:19
 * @author: ztkj-hzb
 * @description: treeview 的 cellfactory 使用 TextFieldTreeCell 效果演示
 **/
public class TreeViewCellFactoryWithTextFieldTreeCellSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();

        //创建TreeView
        TreeView<String> treeView = new TreeView<>();

        //准备数据

        //创建根节点
        TreeItem<String> root = new TreeItem<>("中国");

        TreeItem<String> hb = new TreeItem<>("湖北省");
        TreeItem<String> hb_1 = new TreeItem<>("鄂州市");
        TreeItem<String> hb_2 = new TreeItem<>("武汉市");
        TreeItem<String> hb_3 = new TreeItem<>("襄樊市");
        TreeItem<String> hb_4 = new TreeItem<>("黄冈市");

        //添加子节点
        hb.getChildren().addAll(hb_1, hb_2, hb_3, hb_4);

        TreeItem<String> gd = new TreeItem<>("广东省");
        TreeItem<String> gd_1 = new TreeItem<>("广州市");
        TreeItem<String> gd_2 = new TreeItem<>("深圳市");
        TreeItem<String> gd_3 = new TreeItem<>("东莞市");
        gd.getChildren().addAll(gd_1, gd_2, gd_3);

        TreeItem<String> hn = new TreeItem<>("湖南省");
        TreeItem<String> hn_1 = new TreeItem<>("长沙市");
        TreeItem<String> hn_2 = new TreeItem<>("岳阳市");
        hn.getChildren().addAll(hn_1, hn_2);

        root.getChildren().addAll(hb, gd, hn);

        //设置根节点
        treeView.setRoot(root);

        //设置宽高
        treeView.setPrefSize(300, 300);

        //设置是否展开子节点列表
        root.setExpanded(true);


        anchorPane.getChildren().addAll(treeView);
        AnchorPane.setTopAnchor(treeView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        //设置编辑单元格样式
        treeView.setEditable(true);
        treeView.setCellFactory(TextFieldTreeCell.forTreeView());


    }
}
